package com.atguigu.spzx.service.impl;

import com.atguigu.spzx.mapper.SysRoleMapper;
import com.atguigu.spzx.mapper.SysRoleUserMapper;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.service.SysRoleService;
import com.atguigu.spzx.service.SysRoleUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Override
    public PageInfo<SysRole> findPage(SysRoleDto sysRoleDto, Integer pageNum, Integer pageSize) {
        //开启分页功能
        //底层，其实将分页信息封装Page对象，利用ThreadLocal绑定当前线程上
        PageHelper.startPage(pageNum, pageSize);
        Page<SysRole> page = sysRoleMapper.findPage(sysRoleDto);
        //默认导航页是8
        //return new PageInfo<>(page, 5);
        return new PageInfo<>(page);
    }

    @Override
    public void saveRole(SysRole sysRole) {
        sysRoleMapper.insert(sysRole);
    }

    @Override
    public void updateRole(SysRole sysRole) {
        sysRoleMapper.update(sysRole);
    }

    @Override
    public void removeById(Long id) {
        sysRoleMapper.removeById(id);
    }

    @Override
    public Map<String, Object> findAllRoles(Long id) {
        List<SysRole> list = sysRoleMapper.findAllRoles();
        List<Long> sysRoles = sysRoleUserMapper.findSysUserRoleByUserId(id);
        Map<String, Object> map = new HashMap<>();
        map.put("allRolesList", list);
        map.put("sysUserRoles", sysRoles);
        return map;
    }
}
